/**
 * table 操作项混入
 * */
import {
  ApiRequest
} from '@api/basePage';
import {
  getBtnsId
} from '@/libs/btnsPermission'
export default {
  props: {
    // UUID可以通过组件传递
    uuid: {
      type: String
    },
    // 表格上面的操作按钮
    tableTopOperateData: {
      type: Array
    },
    // 表格上面的批量操作按钮
    tableTopBatchOperate: {
      type: Array
    },
    // 表格已选中的行数据
    selectedData: {
      type: Array
    },
    basePath: {
      type: String
    },
    // 请求接口
    requestApi: {
      type: Object
    },
    type: {
      type: Object
    }
  },
  data () {
    return {
      tableBtn: [],
      tableBatchBtns: [],
      tableSelectData: [],
      jumpPath: ''
    }
  },
  watch: {
    tableTopOperateData: function (n, o) {
      this.tableBtn = n
    },
    tableTopBatchOperate: function (n, o) {
      this.tableBatchBtns = n
    },
    selectedData: function (n, o) {
      this.tableSelectData = n;
    },
    basePath: function (n, o) {
      this.jumpPath = n
    },
    type: function (n, o) {
      this.tableSelectData = []
    }
  },
  created () {
    this.tableBtn = this.tableTopOperateData;
    this.tableBatchBtns = this.tableTopBatchOperate;
    this.tableSelectData = this.selectedData;
    this.jumpPath = this.basePath;
  },
  methods: {
    // 获得选中项的uuid集合
    getUuids (uuid = 'uuid') {
      let uuidArr = []

      this.tableSelectData.forEach(item => {
        uuidArr.push(item[uuid])
      });

      return uuidArr.join(',');
    },
    // 表格操作项事件
    tableTopOperate (name) {
      if (name === 'export') {
        this.$emit('exportData');
      } else if (name === 'add') {
        let curBtnId = getBtnsId('新增')
        sessionStorage.setItem('buttonId', curBtnId)
        let jumpPath = this.jumpPath + 'Add';
        if (this.uuid) {
          this.$router.push({
            path: jumpPath,
            query: {
              uuid: this.uuid
            }
          })
        } else {
          this.$router.push({
            path: jumpPath
          })
        }
      } else if (name === 'pSub') {
        this.$emit('pSub');
      } else {
        let curBtnId = getBtnsId('修改')
        sessionStorage.setItem('buttonId', curBtnId)
        let jumpPath = this.jumpPath + name;
        if (this.uuid) {
          this.$router.push({
            path: jumpPath,
            query: {
              uuid: this.uuid
            }
          })
        } else {
          this.$router.push({
            path: jumpPath
          })
        }
      }
    },
    // table 操作列点击事件
    handleClickItem (name) {
      if (name === 'delete') {
        let data = {
          uuids: this.getUuids()
        }
        // 开始请求列表
        ApiRequest(this.requestApi.baseDelTableList.url, this.requestApi.baseDelTableList.type, data, true).then(res => {
          if (res.retStatus === '1') {
            this.$Message.success('批量删除成功');
            this.tableSelectData = [];
            this.actionsChange();
          }
        })
      }
    },
    // 父级重新请求接口
    actionsChange () {
      this.$emit('actionsChange')
    }
  }
}
